﻿using DeepZoom;
using VirtualSlideViewer.TileSources;
using MahApps.Metro.Controls;
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows;
using IOPath = System.IO.Path;

namespace VirtualSlideViewer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            string fileName = ShowOpenFileDialog();
            if (string.IsNullOrEmpty(fileName))
            {
                this.Close();
                return;
            }

            InitializeComponent();

            MultiScaleTileSource tileSource;
            try
            {
                tileSource = GetTileSource(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open this file!" + (!string.IsNullOrEmpty(ex.Message) ? "\r\n" + ex.Message : ""));
                this.Close();
                return;
            }
            DeepZoomControl.Source = tileSource;
            Title = System.IO.Path.GetFileName(fileName) + " - VirtualSlideViewer";
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            string fileName = ShowOpenFileDialog();
            if (string.IsNullOrEmpty(fileName))
            {
                return;
            }

            MultiScaleTileSource tileSource;
            try
            {
                tileSource = GetTileSource(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to open this file!" + (!string.IsNullOrEmpty(ex.Message) ? "\r\n" + ex.Message : ""));
                return;
            }

            var original = DeepZoomControl.Source;
            DeepZoomControl.Source = GetTileSource(fileName);
            (original as IDisposable)?.Dispose();
            original = null; ;

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.WaitForFullGCComplete();
            GC.Collect();

            Title = System.IO.Path.GetFileName(fileName) + " - VirtualSlideViewer";
        }

        private string ShowOpenFileDialog(Window owner = null)
        {
            var dialog = new OpenFileDialog();
            dialog.DefaultExt = ".dzi";
            dialog.Filter = "所有支持的类型(*.tif;*.tiff;*.svs;*.vms;*.vmu;*.ndpi;*.scn;*.mrxs;*.svslide;*.bif;*.dzi)|*.tif;*.tiff;*.svs;*.vms;*.vmu;*.ndpi;*.scn;*.mrxs;*.svslide;*.bif;*.dzi|Aperio (*.svs;*.tif)|*.svs;*.tif|Hamamatsu (*.vms;*.vmu;*.ndpi)|*.vms;*.vmu;*.ndpi|Leica (*.scn)|*.scn|MIRAX (*.mrxs)|*.mrxs|Philips (*.tiff)|*.tiff|Sakura (*.svslide)|*.svslide|Trestle (*.tif)|*.tif|Ventana (*.bif;*.tif)|*.bif;*.tif|Generic tiled TIFF (*.tif;*.tiff)|*.tif;*.tiff|Deep Zoom (*.dzi)|*.dzi|所有文件(*.*)|*.*";
            var ret = owner == null ? dialog.ShowDialog() : dialog.ShowDialog(owner);
            if (!ret.HasValue || !ret.Value)
            {
                return null;
            }
            return dialog.FileName;
        }

        private MultiScaleTileSource GetTileSource(string filePath)
        {
            var fileInfo = new FileInfo(filePath);
            if (".dzi".Equals(fileInfo.Extension, StringComparison.OrdinalIgnoreCase))
            {
                var ifpFileName = IOPath.GetFileNameWithoutExtension(filePath) + "_files.ifp";
                var ifpPath = IOPath.Combine(fileInfo.DirectoryName, ifpFileName);
                if (File.Exists(ifpPath))
                {
                    return new DeepZoomIfpTileSource(new Uri(filePath));
                }
                else
                {
                    return new DeepZoomImageTileSource(new Uri(filePath));
                }
            }
            else
            {
                return new OpenSlideTileSource(filePath);
            }

        }
    }
}
